package bgg4j;



import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import bgg4j.http.HttpClient;


public class Pgdb {
    HttpClient http = null;
    private String baseURL = "http://www.gamers-jp.com/playgame/";
    public Pgdb() {
        http = new HttpClient();
    }
    
/*    public final synchronized void getFile(String url, String filepath) throws
    BggException, IOException {
    	if (url != null){
    		http.getFile(url, filepath);
    	}
    }*/
    
    public final synchronized List<String> getJapaneseTitle(String key, String year, String playerA, String playerB) throws
    BggException {
    	List<String> list = new ArrayList<String>(0);
    	StringBuffer url;
    	

    	url = new StringBuffer(baseURL + "db_searchlist.php?search_str=" +  key);
    	if (year!=null){
    		String strYear = "&relyear=" + year + "&relyearb=" + year;
    		url.append(strYear);
    	}
    	if (playerA != null && playerB != null){
    		String strPlayer = "&player=" +  playerA + "&playerb=" + playerB;
    		url.append(strPlayer);
    	}
    	
    	
    	BufferedReader br = new BufferedReader(http.get(url.toString(),"SJIS").asReader());
    	String line;
    	try {
			while(null !=(line = br.readLine())){
				if (line.indexOf("TITLE") > 0){
					if (line.indexOf("play:game") < 0){
						list.add(line.substring(line.indexOf("<TITLE>")+7,line.indexOf("</TITLE>")));
						break;
					}
				}
				if (line.indexOf("game_id") > 0){
					list.add(line.substring((line.lastIndexOf(">", line.lastIndexOf("</a>"))+1),line.lastIndexOf("</a>")));
				}
			}
			br.close();
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

    	return list;
    }

}
